import { Router } from "@angular/router";
import {HttpService} from '../../../services/http.service';
import { BaseUiService } from '../../../services/base-ui.service';
import { LoginInfoService } from '../../../services/login-info.service';
import { commService } from '../../../services/comm.service';
import {EventService } from '../../../services/QaService';
import { WebsocketService } from '../../../services/websocket.service';
import {Component,ChangeDetectorRef} from '@angular/core';
import { NavParams } from '@ionic/angular';

@Component({
  selector: "faceRecognitiono",
  templateUrl: "./faceRecognition.html",
  styleUrls: ["./faceRecognition.scss"]
})
export class faceRecognition{
  public notaryList:any;
  public info:any
  public notaryId = '';
  public relayCode="";
  private modelContent="";
  private flag=true;
  private falgIndex=1;
  public applyInfo = {
    userName:"",
    gender:'',
    birthday:'',
    idCard:'',
    mobile:'',

  };
  public userInfo= {
    userName:"",
    gender:'',
    birthday:'',
    idCard:'',
    mobile:'',
  };
  //   this.cd.detectChanges();
  constructor(
    public navParams: NavParams,
    public LoginInfoService: LoginInfoService,
    public router: Router, 
    public HttpService: HttpService,
    public baseUiService: BaseUiService,
    public commService:commService,
    public eventService:EventService,
    public wsCoket: WebsocketService,
    private cd: ChangeDetectorRef,
    public httpService:HttpService,

    ) {
      this.userInfo = this.LoginInfoService.getInfo();
      console.log("-------------userInfo--------------------")
      console.log(this.userInfo)
  }

    ngOnInit() {
      setTimeout(()=>{
          this.faceImg();//延迟加载
      },270)
    }

    async close(modelContent){
      this.commService.stopFaceCollect();
      this.navParams.data.modal.dismiss({
          code: modelContent || '404'
      });
    }

    faceImg(){
      let imgIndex=1;
      this.commService.startFaceCollect().subscribe(res=>{
        imgIndex++;
        if(this.falgIndex==1)    this.baseUiService.showToast('请正面对准镜头，正在为您活体检测....','warning','top');
        console.log("diaoyong",imgIndex)
        if(imgIndex % 10==0){//处理返回帧数
          console.log("jiehsu")
            if(this.flag && this.falgIndex < 3){
              this.flag=false;//单例 查询模式
                // this.baseUiService.showToast('人脸比对中....','warning','top');
                this.imgContrast(res);
            
            }else if(this.falgIndex == 3 && this.flag){
              this.baseUiService.showToast('活体检测失败,关闭人脸','warning','top');
              this.flag=false;//单例 查询模式
              this.close('404');
            }
        }
      });
  
    }
  
    /**
     * @memberOf TransactionPage
    * */
    imgContrast(imgBase64){
      let params= {
        "image":imgBase64,
        "name":this.userInfo.userName||"",
        "idcard":this.userInfo.idCard||""
      }
      console.log(params)
      this.httpService.imgContrast(params).then(res=>{
        this.falgIndex++;
        console.log("-----------result----------", this.falgIndex)
        this.flag=true;//单例 查询模式
      if(res['result']== null){
          this.baseUiService.showToast('请正面对准镜头，正在为您活体检测','warning','top')
        }else  if(Math.floor(res['result']) > 70){
          this.baseUiService.showToast('人脸比对成功','customblue','top')
          this.flag=false;//单例 查询模式
          this.close('200');
        }else  if(Math.floor(res['result']) < 70){
          this.baseUiService.showToast('相似度为'+res['result']+"%,比对失败",'warning','top')
        }else if(res['error_code']=='222202'){
          this.baseUiService.showToast('请正面对准镜头，正在为您活体检测','warning','top')
        }else if(res['error_code']=='222351'){
          this.baseUiService.showToast('身份证与姓名不匹配','warning','top')
          this.flag=false;//单例 查询模式
          this.close('200');
        }else if(res['error_code']=='17'){
          this.baseUiService.showToast('公安联网异常，请求公安联网核查失败！','warning','top')
          this.flag=false;//单例 查询模式
          this.close('200');
        }
        this.cd.detectChanges();
      })
    }
        
}
